package com.noble.glamour.bean.my;


import android.os.Parcel;
import android.os.Parcelable;

import com.noble.library.base.bean.BaseData;
import com.noble.library.utils.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * 礼物列表
 */
public class GiftsList extends BaseData {
    private List<GiftInfo> giftList;

    @Override
    public void parseJson(String s) {
        JSONArray jsonArray = getJsonArray(s);
        giftList = (List<GiftInfo>) getBaseDataList(jsonArray, GiftInfo.class);
    }

    public List<GiftInfo> getGiftList() {
        return giftList;
    }

    /**
     * 根据礼物id获取礼物列表中的信息
     *
     * @param id 礼物id
     * @return 从礼物列表中获取到的单个礼物信息
     */
    public GiftInfo getGiftInfo(int id) {
        if (giftList != null && !giftList.isEmpty()) {
            for (GiftInfo giftInfo : giftList) {
                if (giftInfo.getId() == id) return giftInfo;
            }
        }
        return null;
    }

    /**
     * 单个礼物信息
     */
    public static class GiftInfo extends BaseData implements Parcelable {
        private int id = 0;
        private String name = "";
        private String pic = "";
        private int money = 0;

        private boolean isSelect = false;   // 是否被选中
        private int num;        // 赠送数量
        private String timeCreate;

        @Override
        public void parseJson(String s) {
            JSONObject jsonObject = getJsonObject(s);
            this.setId(jsonObject.optInt("Id"));
            this.setName(jsonObject.optString("Name"));
            this.setPic(jsonObject.optString("Image"));
            this.setMoney(jsonObject.optInt("DoSpend"));

            this.setNum(jsonObject.optInt("NumGift"));
            this.setTimeCreate(jsonObject.optString("TimeCreate"));
        }

        public String getTimeCreate() {
            return StringUtils.getCreateTime(timeCreate);
        }

        public void setTimeCreate(String timeCreate) {
            this.timeCreate = timeCreate;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public boolean isSelect() {
            return isSelect;
        }

        public void setIsSelect(boolean isSelect) {
            this.isSelect = isSelect;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getMoney() {
            return money;
        }

        public void setMoney(int money) {
            this.money = money;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "GiftInfo{" +
                    "isSelect=" + isSelect +
                    ", pic='" + pic + '\'' +
                    ", name='" + name + '\'' +
                    ", money=" + money +
                    ", id=" + id +
                    '}';
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(this.id);
            dest.writeString(this.name);
            dest.writeString(this.pic);
            dest.writeInt(this.money);
            dest.writeByte(this.isSelect ? (byte) 1 : (byte) 0);
            dest.writeInt(this.num);
        }

        public GiftInfo() {
        }

        protected GiftInfo(Parcel in) {
            this.id = in.readInt();
            this.name = in.readString();
            this.pic = in.readString();
            this.money = in.readInt();
            this.isSelect = in.readByte() != 0;
            this.num = in.readInt();
        }

        public static final Parcelable.Creator<GiftInfo> CREATOR = new Parcelable.Creator<GiftInfo>() {
            @Override
            public GiftInfo createFromParcel(Parcel source) {
                return new GiftInfo(source);
            }

            @Override
            public GiftInfo[] newArray(int size) {
                return new GiftInfo[size];
            }
        };
    }
}
